#/***************************************************************************\
# * Copyright (C) by Keio University
# * CMakeLists.txt created in 11 2011.
# * Mail : fdesorbi@hvrl.ics.keio.ac.jp
# *
# * This file is part of the HVRL Engine Library.
# *
# * The HVRL Engine Library is free software; you can redistribute it and/or modify
# * it under the terms of the GNU Lesser General Public License as published by
# * the Free Software Foundation; either version 3 of the License, or
# * (at your option) any later version.
# *
# * The HVRL Engine Library is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU Lesser General Public License for more details.
# *
# * You should have received a copy of the GNU Lesser General Public License
# * along with this program.  If not, see <http://www.gnu.org/licenses/>.
# *
# ***************************************************************************/
#create_module(gpgpu hvrl_common)
#created thanks to OpenCV GPU module's cmake file
SET(name gpgpu)
SET(the_target "hvrl_gpgpu")
PROJECT(${the_target})

set(DEPS "hvrl_common")

SET(HVRL_INCLUDE_DIRS ${HVRL_INCLUDE_DIRS} "${CMAKE_CURRENT_SOURCE_DIR}/include" CACHE INTERNAL "")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include"
		    "${CMAKE_CURRENT_SOURCE_DIR}/src/cuda"
		    "${CMAKE_CURRENT_SOURCE_DIR}/src"
		    "${CMAKE_CURRENT_BINARY_DIR}")


    foreach(d ${DEPS})
        if(d MATCHES "hvrl_")
            string(REPLACE "hvrl_" "${HVRL_SOURCE_DIR}/modules/" d_dir ${d})
            if (EXISTS "${d_dir}/include")
                INCLUDE_DIRECTORIES("${d_dir}/include")
            endif()
        endif()
    endforeach()

file(GLOB lib_srcs "src/*.cpp")
file(GLOB lib_cuda "src/cuda/*.cu")
file(GLOB lib_cl "src/opencl/*.cpp")
FILE(GLOB lib_hdrs "include/hvrl/${name}/cuda/*.hpp")
FILE(GLOB lib_cuda_hdrs "include/hvrl/${name}/*.cuh")
FILE(GLOB lib_cl_hdrs "include/hvrl/${name}/opencl/*.hpp")

source_group("Sources" FILES ${lib_srcs} ${lib_cuda} ${lib_cl})
source_group("Headers" FILES ${lib_int_hdrs} ${lib_cuda_hdrs} ${lib_cl_hdrs})

if (CUDA_FOUND)

    if (UNIX OR APPLE)
        set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS}  "-Xcompiler;-fPIC;")
        #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" "-fPIC")
    endif()

    #set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "-keep")
    #set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "-Xcompiler;/EHsc-;")
    set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "-arch;sm_20")

   # if (APPLE)
   #     set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "-Xcompiler;-fno-finite-math-only;")
   # endif()


    #string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
    #string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
    #string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")

    #if(MSVC)
        #string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
        #string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
        #string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
     #   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4211 /wd4201 /wd4100 /wd4505 /wd4408")

      #  string(REPLACE "/EHsc-" "/EHs" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
       # string(REPLACE "/EHsc-" "/EHs" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
       # string(REPLACE "/EHsc-" "/EHs" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
       # string(REPLACE "/EHsc-" "/EHs" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
       # string(REPLACE "/EHsc-" "/EHs" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
       # string(REPLACE "/EHsc-" "/EHs" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
   # endif()

    #if (BUILD_SHARED_LIBS)
	#	set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "-Xcompiler;-DCVAPI_EXPORTS")
	#endif()
    #CUDA_COMPILE(cuda_objs ${lib_cuda} ${lib_cuda_hdrs})
	

#add_library(${the_target} ${lib_srcs} ${lib_hdrs} ${cuda_objs} ${lib_cuda_hdrs} ${lib_cl_hdrs} ${lib_cl})

IF(HVRL_BUILD_SHARED_LIBS)
        #IF(MSVC)
            set_target_properties(${the_target} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
        #ELSE()
            add_definitions(-DCVAPI_EXPORTS)        
        #ENDIF()
	cuda_ADD_LIBRARY(${the_target} ${lib_srcs} ${lib_hdrs} ${lib_cuda} ${lib_cuda_hdrs} ${lib_cl_hdrs} ${lib_cl})
ELSE()
	cuda_ADD_LIBRARY(${the_target} STATIC ${lib_srcs} ${lib_hdrs} ${lib_cuda} ${lib_cuda_hdrs} ${lib_cl_hdrs} ${lib_cl})
ENDIF()

endif()

TARGET_LINK_LIBRARIES(${the_target} ${HVRL_EXTERN_LIBS} ${DEPS})

SET_TARGET_PROPERTIES(${the_target} PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${HVRL_LIB_INSTALL_PATH}
        RUNTIME_OUTPUT_DIRECTORY ${HVRL_BIN_INSTALL_PATH}
        INSTALL_NAME_DIR lib)

INSTALL(TARGETS ${the_target}
        RUNTIME DESTINATION ${HVRL_BIN_INSTALL_PATH}
        LIBRARY DESTINATION ${HVRL_LIB_INSTALL_PATH} COMPONENT main
        ARCHIVE DESTINATION ${HVRL_LIB_INSTALL_PATH} COMPONENT main)

SET(HVRL_LIBS ${HVRL_LIBS} ${the_target} CACHE INTERNAL "")

INSTALL(FILES ${lib_hdrs} DESTINATION ${HVRL_INCLUDE_INSTALL_PATH}/hvrl/${name}/)

# Additional target properties
#set_target_properties(${the_target} PROPERTIES
#    RUNTIME_OUTPUT_DIRECTORY ${HVRL_INSTALL_PATH}
#    INSTALL_NAME_DIR lib
#    )

# Add the required libraries for linking:
#target_link_libraries(${the_target} ${IPP_LIBS} ${DEPS} )

#if (CUDA_FOUND)
 #   target_link_libraries(${the_target} ${CUDA_LIBRARIES})
 #   CUDA_ADD_CUFFT_TO_TARGET(${the_target})

  #  unset(CUDA_npp_LIBRARY CACHE)
  #  find_cuda_helper_libs(npp)
  #  target_link_libraries(${the_target} ${CUDA_npp_LIBRARY})
#endif()

#SET_TARGET_PROPERTIES(${the_target} PROPERTIES
 #       ARCHIVE_OUTPUT_DIRECTORY ${HVRL_LIB_INSTALL_PATH}
 #       RUNTIME_OUTPUT_DIRECTORY ${HVRL_BIN_INSTALL_PATH}
 #       INSTALL_NAME_DIR lib)

# Dependencies of this target:
#add_dependencies(${the_target} ${DEPS})

#INSTALL(TARGETS ${the_target}
 #       RUNTIME DESTINATION ${HVRL_BIN_INSTALL_PATH}
 #       LIBRARY DESTINATION ${HVRL_LIB_INSTALL_PATH} COMPONENT main
 #       ARCHIVE DESTINATION ${HVRL_LIB_INSTALL_PATH} COMPONENT main)

#INSTALL(FILES ${lib_hdrs} DESTINATION ${HVRL_INCLUDE_INSTALL_PATH}/hvrl/${name}/)

#SET(HVRL_LIBS ${HVRL_LIBS} "${the_target}" CACHE INTERNAL "")

















